{include file='header'/}
<script src="__LIB__/laydate/laydate.js"></script>
<script type="text/javascript" src="__LIB__/ueditor-plus/ueditor.config.js"></script>
<script type="text/javascript" src="__LIB__/ueditor-plus/ueditor.all.js"></script>
<script type="text/javascript" src="__LIB__/ueditor-plus/lang/zh-cn/zh-cn.js"></script>
<body>
<form class="layui-form" action="javascript:;" onsubmit="return dosub(this)">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label required">所属分类</label>
                    <div class="layui-input-inline">
                        <select name="cate_id" lay-verify="required" style="width:300px;">
                            <option value="">请选择分类</option>
                            {volist name="cates" id="vo"}
                            <option value="{$vo.id}"
                            <?php if($vo['model_id']!=$model_id){echo "disabled";} ?>
                            <?php if($vo['id']==$data['cate_id']){echo "selected";} ?>>
                            {$vo.name|raw}
                            </option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-inline">
                        <input type="number" name="sort" autocomplete="off" placeholder="请输入排序"
                               class="layui-input" value="{$data.sort}">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">标题</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="required" autocomplete="off"
                           placeholder="请输入标题" lay-reqtext="标题不能为空哦" id="titleInput"
                           class="layui-input" value="{$data.title}">
                    <div id="colorpicker" title="设置颜色"></div>
                    <input class="layui-input" type="hidden" name="color" id="title_color" value="{$data.color}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">关键词|标签</label>
                <div class="layui-input-block">
                    <input type="text" id="keyword_name" name="keyword_names" autocomplete="off"
                           placeholder="请选择关键字" class="layui-input" value="{$data.keyword_names}" readonly>
                    <input type="hidden" id="keyword_id" name="keyword_ids" value="{$data.keyword_ids}"
                           autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-block">
                    <textarea name="description" class="layui-textarea">{$data.description}</textarea>
                </div>
                <tip>如不填写，自动提取文章前250字符</tip>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">属性</label>
                <div class="layui-input-block">
                    {foreach name="flags" id="vo" key="k"}
                    <input type="checkbox" name="flag[]" value="{$k}" lay-skin="primary"
                           title="{$vo}[{$k}]" lay-filter='flag' {if (in_array($k,$data['flag_ids']))}checked{/if}>
                    {/foreach}
                </div>
            </div>
            <div class="layui-form-item {if (!in_array(4,$data['flag_ids']))}layui-hide{/if}" id="jumpUrl">
                <label class="layui-form-label required">跳转url</label>
                <div class="layui-input-block">
                    <input type="text" id="jump_url" lay-verify="url" name="jump_url" value="{$data.jump_url}"
                           autocomplete="off"
                           class="layui-input" data-s='0'>
                </div>
                <tip>url必须加“http|https”</tip>
            </div>


            <div class="layui-form-item huicmf-upload">
                <label class="layui-form-label">缩略图</label>
                <div class="layui-input-block">
                    <input type="text" name="thumb" value="{$data.thumb}"
                           id="thumb-select-input" autocomplete="off" class="layui-input">
                    <button type="button" class="layui-btn layui-btn-normal layUpload" id="picker_pic2"
                            data-multiple="false" data-input-id="thumb-select-input"
                            data-preview-id="thumb" data-type="image"><i class="layui-icon"></i>上传图片
                    </button>
                    <button type="button" class="layui-btn ml-10"
                            data-open-pic="{:url('upload_file/index',['type'=>'one','select_id'=>'thumb-select-input'])}">
                        <i class="layui-icon"></i>选择图片
                    </button>
                </div>
                <div class="layui-form-label"></div>
                <div class="layui-input-inline">
                    {notempty name='data.thumb'}
                    <div id="thumb-select-input_box" class="pic_box">
                        <dl>
                            <dt><img src="{$data.thumb}" data-url="{$data.thumb}"></dt>
                            <dd>删除</dd>
                        </dl>
                    </div>
                    {/notempty}
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">是否原创</label>
                    <div class="layui-input-inline">
                        <input type="radio" name="original" value="1" title="是" {if($data.original eq 1)}checked{/if}>
                        <input type="radio" name="original" value="0" title="否" {if($data.original eq 0)}checked{/if}>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">来源|作者</label>
                    <div class="layui-input-inline">
                        <input type="text" name="origin" autocomplete="off" placeholder="请输入来源或作者"
                               class="layui-input" value="{$data.origin}">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">来源url</label>
                <div class="layui-input-block">
                    <input type="text" name="origin_url" lay-verify="url" autocomplete="off" placeholder="请输入来源url"
                           class="layui-input" value="{$data.origin_url}">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">点击量</label>
                    <div class="layui-input-inline">
                        <input type="number" name="click" autocomplete="off" placeholder="请输入点击量"
                               class="layui-input" value="{$data.click}">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-form-item">
                        <label class="layui-form-label">发布时间</label>
                        <div class="layui-input-inline">
                            <input type="text" id="publish_time" name="publish_time" autocomplete="off"
                                   value="{$data.publish_time}" class="layui-input" placeholder="yyyy-MM-dd HH:mm:ss">
                        </div>
                    </div>
                </div>
            </div>

            {$model_fields|raw}

            <div class="layui-form-item">
                <label class="layui-form-label required">内容</label>
                <div class="layui-input-block" id="contentEditorHtml" style="z-index: 0">
                    {php}echo
                    libs\Form::editor('content','container',$data['content'],'width:100%;height:400px');{/php}
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    <input type="checkbox" name="auto_image" value="1" title="提取内容第一张图片为缩略图"
                           lay-skin="primary">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="radio" name="status" value="1" title="发布" {if($data.status eq 1)}checked{/if}>
                    <input type="radio" name="status" value="0" title="草稿" {if($data.status eq 0)}checked{/if}>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <input type="hidden" id="id" name="id" value="{$data.id}">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="admin-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

{include file='footer'/}
<script>
  layui.use(['form', 'jquery', 'colorpicker', 'laydate', 'upload_hui', 'tagpicker', 'ddSort'], function () {
    let form = layui.form;
    let $ = layui.jquery;
    var colorpicker = layui.colorpicker;
    let upload_hui = layui.upload_hui;
    let tagspicker = layui.tagpicker;
    var laydate = layui.laydate;

    // 请求分词
    $('#titleInput').on('blur', function () {
      var title = $(this).val();
      if (title.length > 0) {

        $.post("/api.php/api/get_keywords", {title: title}, function (res) {
          let keywordInput = $('#keyword_name').val();
          if (res.code === 1 && keywordInput.length === 0) {
            $('#keyword_name').val(res.data)
            $('#keyword_id').val(res.data)
          }
        });
      }
    })

    //颜色选择器
    colorpicker.render({
      elem: '#colorpicker'  //绑定元素
      , color: '{$data.color}'
      , alpha: true //开启透明度
      , format: 'rgb'
      , predefine: true // 开启预定义颜色
      , done: function (color) {
        $('#title_color').val(color);
      }
    });

    //单图上传
    upload_hui.render();

    // 图片列表拖动，需要引入ddSort
    $('.uploader-list').DDSort({
      target: '.file-item',
      delay: 100, // 延时处理，默认为 50 ms，防止手抖点击 A 链接无效
      floatStyle: {
        'border': '1px solid #ccc',
        'background-color': '#fff'
      }
    });

    //日期时间选择器
    laydate.render({
      elem: '#publish_time'
      , type: 'datetime'
    });

    form.on('checkbox(flag)', function (obj) {
      var arr_box = [];
      $('input[type=checkbox]:checked').each(function () {
        arr_box.push($(this).val());
      });
      if (arr_box.includes('4')) {
        $('#jumpUrl').removeClass('layui-hide');
        $("#jumpUrl>div>input").attr('data-s', '1');
      } else {
        $('#jumpUrl').addClass('layui-hide');
        $("#jumpUrl>div>input").attr('data-s', '0');
      }
    });

    var tags = new tagspicker({
      'url': "{:url('keywords/get_keyword_cate')}",
      'target': 'keyword_name',
      'tag_ids': 'keyword_id',
      'tag_tags': 'keyword_name',
      'height': 500,
      'isDiy': 1
    });
    form.render();

    window.dosub = function (obj) {
      $.ajax({
        url: "{:url('content/edit')}",
        data: $(obj).serialize(),
        dataType: 'json',
        type: 'post',
        success: function (res) {
          if (res.code === 200) {
            hui_msg(res.msg, 1, function () {
              layer.confirm('是否关闭页面？', {
                btn: ['关闭页面', '留在此页面']
              }, function () {
                var pageId = parent.layer.getFrameIndex(window.name);
                parent.layer.close(pageId);
                parent.layui.table.reload('testReload');
              }, function () {
                window.location.reload();
              });
            });
          } else {
            hui_msg(res.msg, 2);
          }
        }
      })
      return false;
    };
  })
</script>
<script>
</script>
</body>
</html>
